#!/usr/bin/env bash


help(){
    cat<<__EOF__
Usage: m hostname [OPTION] [newhostname]

Description: Manage the hostname of the system. This includes ComputerName, HostName, LocalHostName, and NetBIOSName.

Options:
  --help       Show this help message and exit


Examples:
  m hostname                # get the current hostname information (computername, hostname, localhostname and netbiosname)
  m hostname newhostname    # set a new hostname (computername, hostname, localhostname, netbiosname)
__EOF__
}


show_hostname(){
    scutil --get ComputerName | awk '{print "ComputerName: ", $1}'
    scutil --get HostName | awk '{print "HostName: ", $1}'
    scutil --get LocalHostName | awk '{print "LocalHostName: ", $1}'
    defaults \
        read /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName | \
        awk '{print "NetBIOSName: " $1}'

}

set_hostname(){
    sudo scutil --set ComputerName "$1" && \
        sudo scutil --set HostName "$1" && \
        sudo scutil --set LocalHostName "$1" && \
        sudo defaults \
        write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName \
        -string "$1"
    echo "Hostname changed to: ${1}"
}


case $1 in
    --help)
        help
        ;;
    *)
        if [ -z "$1" ]; then
            show_hostname
        else
            set_hostname $1
        fi
        ;;
esac
